package com.github.m6d21.conflict.main;

import com.github.m6d21.conflict.config.ConflictScanConfigReader;
import com.github.m6d21.conflict.config.ConflictScanConfigReader.ScanConfig;
import com.github.m6d21.conflict.util.CloseUtil;
import com.github.m6d21.conflict.util.ConflictScanHelper;
import com.github.m6d21.conflict.util.FileUtil;
import java.io.InputStream;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *<p>
 *jar包Main类
 *</p>
 *
 * @author liuyang
 * @date 2018/12/12 Wed 14:46:00
 * @since 1.0.0
 */
public class ConflictScanMain {

    private static final Logger LOG = LoggerFactory.getLogger(ConflictScanMain.class);

    private static final String DEFAULT_SCAN_CONFIG = "default-conflict-scan.properties";

    public static void main(String[] args) {
        tip();

        ConflictScanConfigReader reader = ConflictScanConfigReader.getInstance();
        if (args != null && args.length > 0) {
            reader.propertiesPath(args[0]);
        }

        //扫描配置文件
        ScanConfig config = reader.getScanConfig();
        //注入配置
        ConflictScanHelper helper = ConflictScanHelper.getInstance();
        if (config.getScanPaths() != null) {
            helper.paths(config.getScanPaths());
        }
        if (config.getScanPackages() != null) {
            helper.forPackages(config.getScanPackages());
        }
        if (config.getOnlyShowDiffClass() != null) {
            helper.onlyShowDiffClass(config.getOnlyShowDiffClass());
        }
        String[] includeKeyword = null;
        if (config.getIncludeKeyword() != null) {
            includeKeyword = config.getIncludeKeyword();
        }
        String[] excludeKeyword = null;
        if (config.getExcludeKeyword() != null) {
            excludeKeyword = config.getExcludeKeyword();
        }
        if (includeKeyword != null || excludeKeyword != null) {
            helper.filter(includeKeyword, excludeKeyword);
        }
        //开始扫描
        helper.startScanConflict();
    }

    /**
     * 提示文案
     */
    private static void tip() {
        System.out.println("你可以新建一个properties文件放到当前目录，按照下面提示进行配置，否则，将按默认配置扫描，配置内容如下：");
        Scanner sc = null;
        InputStream inputStream = null;
        try {
            inputStream = ConflictScanMain.class.getClassLoader().getResource(DEFAULT_SCAN_CONFIG).openStream();
            System.out.println();
            String tips = FileUtil.readTextFile(inputStream);
            System.out.println(tips);
            System.out.print("确认开始扫描(y/n, y开始, n退出)：");
            sc = new Scanner(System.in);
            while (sc.hasNext()) {
                String input = sc.next();
                if ("y".equals(input)) {
                    break;
                } else if ("n".equals(input)) {
                    System.exit(0);
                }
                System.out.print("确认开始扫描(y/n, y开始, n退出)：");
            }
        } catch (Exception e) {
            LOG.error("启动程序时遇到异常：{}", e);
        } finally {
            CloseUtil.close(sc);
            CloseUtil.close(inputStream);
        }
    }
}
